﻿using PBO.Data;
using PBO.Game.BattleClass;
using PokemonBattle.BattleNetwork;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Resources;
using System.Windows.Forms;
using System.Linq;

namespace PBO.Game.Img
{
    public static class ImageHelper
    {

        #region font

        public static readonly string FONT_NAME = "宋体";

        #endregion

        #region image

        public static Bitmap GetPMStateImg(int index, Pokemon pm, bool showHP = false)
        {
            if (pm.HealthPower == 0)
                return null;
            return GetPMStateImg(index, pm.NickName, pm.MaxHp, pm.HealthPower, pm.Gender, pm.LV, pm.State, showHP);
        }

        /// <summary>
        /// 观战用
        /// </summary>
        public static Bitmap GetPMStateImg(int index, PokemonSnapshot snapshot)
        {
            if (snapshot.Hp == 0)
                return null;
            return GetPMStateImg(index, snapshot.Nickname, snapshot.MaxHp, snapshot.Hp, (PokemonGender)snapshot.Gender, snapshot.Lv, (PokemonState)snapshot.State, false);
        }

        private static Bitmap GetPMStateImg(int index, string nickname, int maxHp, int hp, PokemonGender gender, int lv, PokemonState state, bool showHP)
        {
            Bitmap newImg = default(Bitmap);
            Font font = new Font(FONT_NAME, 12, FontStyle.Regular, GraphicsUnit.Pixel);
            switch (index)
            {
                case 1:
                    if (showHP)
                    {
                        newImg = new Bitmap(Properties.Resources.PMState1);
                    }
                    else
                    {
                        newImg = new Bitmap(Properties.Resources.PMState3);
                    }
                    using (Graphics graph = Graphics.FromImage(newImg))
                    {
                        graph.DrawString(nickname, font, Brushes.Black, 12, 3);
                        graph.DrawString("Lv" + lv, font, Brushes.Black, 82, 5);
                        graph.DrawImageUnscaled(DrawHealthPower(maxHp, hp), 47, 19);
                        switch (gender)
                        {
                            case PokemonGender.Male:
                                graph.DrawImageUnscaled(Properties.Resources.male, 76, 7);
                                break;
                            case PokemonGender.Female:
                                graph.DrawImageUnscaled(Properties.Resources.female, 76, 7);
                                break;
                        }

                        Point pt = new Point(14, 15);
                        if (showHP)
                            pt = new Point(14, 22);

                        if (state != PokemonState.No)
                        {
                            graph.DrawImageUnscaled(state.GetStateImg(), pt);
                        }

                        if (showHP)
                            graph.DrawString(hp + "/" + maxHp, font, Brushes.Black, 70, 25);

                    }

                    break;
                default:
                    newImg = new Bitmap(Properties.Resources.PMState2);
                    using (Graphics graph = Graphics.FromImage(newImg))
                    {
                        graph.DrawString(nickname, font, Brushes.Black, 7, 3);
                        graph.DrawString("Lv" + lv, font, Brushes.Black, 75, 5);
                        switch (gender)
                        {
                            case PokemonGender.Male:
                                graph.DrawImageUnscaled(Properties.Resources.male, 69, 7);
                                break;
                            case PokemonGender.Female:
                                graph.DrawImageUnscaled(Properties.Resources.female, 69, 7);
                                break;
                        }

                        graph.DrawImageUnscaled(DrawHealthPower(maxHp, hp), 41, 19);

                        var stateImg = state.GetStateImg();
                        if (stateImg != null)
                        {
                            graph.DrawImageUnscaled(stateImg, 9, 15);
                        }
                    }

                    break;
            }
            return newImg;
        }

        public static Bitmap GetPMImage(Pokemon pmVal)
        {
            Bitmap pmImg = new Bitmap(126, 46);
            Font font1 = new Font(FONT_NAME, 12, FontStyle.Regular, GraphicsUnit.Pixel);
            Font font2 = new Font(FONT_NAME, 10, FontStyle.Regular, GraphicsUnit.Pixel);

            using (Graphics graph = Graphics.FromImage(pmImg))
            {
                graph.DrawImageUnscaledAndClipped(pmVal.Icon, new Rectangle(8, 10, 32, 32));
                graph.DrawImageUnscaled(DrawHealthPower(pmVal.MaxHp, pmVal.HealthPower), 50, 22);

                switch (pmVal.Gender)
                {
                    case PokemonGender.Male:
                        graph.DrawImageUnscaled(Properties.Resources.male, 110, 10);
                        break;
                    case PokemonGender.Female:
                        graph.DrawImageUnscaled(Properties.Resources.female, 110, 10);
                        break;
                }

                graph.DrawString(pmVal.NickName, font1, Brushes.Black, 40, 9);
                graph.DrawString(pmVal.HealthPower + "/" + pmVal.MaxHp, font2, Brushes.Black, 80, 30);
                if (pmVal.State == PokemonState.No)
                {
                    graph.DrawString("Lv:" + pmVal.LV, font2, Brushes.Black, 45, 30);
                }
                else
                {
                    var stateImg = pmVal.State.GetStateImg();
                    if (stateImg != null)
                    {
                        graph.DrawImageUnscaled(stateImg, 45, 30);
                    }
                }
            }
            return pmImg;
        }


        #region DrawHealthPower

        public static Bitmap DrawHealthPower(double percentage)
        {
            var line = new Bitmap(Properties.Resources.HPLine);

            if (percentage > 0)
            {
                int width = (int)Math.Truncate((double)50 * percentage);
                width = Math.Max(1, width);
                Color color = Properties.Resources.HpColor.GetPixel(50 - width, 0);
                Color color2 = Properties.Resources.HpColor.GetPixel(50 - width, 1);

                Rectangle rect1 = new Rectangle(16, 1, width, 2);
                Rectangle rect2 = new Rectangle(16, 3, width, 2);
                LinearGradientBrush brush1 = new LinearGradientBrush(rect1, color2, color, LinearGradientMode.Vertical);
                LinearGradientBrush brush2 = new LinearGradientBrush(rect2, color, color2, LinearGradientMode.Vertical);
                using (Graphics painter = Graphics.FromImage(line))
                {
                    painter.FillRectangle(brush1, rect1);
                    painter.FillRectangle(brush2, rect2);
                }
            }
            return line;
        }

        public static Bitmap DrawHealthPower(int maxHP, int hp)
        {
            double percentage = (double)hp / (double)maxHP;
            return DrawHealthPower(percentage);
        }

        #endregion

        public static Bitmap GetIndexImage(BattleGroup myGroup)
        {
            Bitmap newImg = new Bitmap(256, 25);
            using (Graphics graph = Graphics.FromImage(newImg))
            {
                var pokemons = myGroup.AllPokemon.ToArray();
                var pokemons2 = myGroup.Opponent.AllPokemon.ToArray();

                for (int i = 0; i < pokemons.Length; i++)
                {
                    if (pokemons[i].HealthPower == 0)
                    {
                        if (pokemons.Length > 6)
                        {
                            graph.DrawImageUnscaled(Properties.Resources.pmball2c, Properties.Resources.pmball2c.Width * i, 8);
                        }
                        else
                        {
                            graph.DrawImageUnscaled(Properties.Resources.pmballc, Properties.Resources.pmballc.Width * i, 3);
                        }
                    }
                    else if (pokemons[i].State != PokemonState.No)
                    {
                        if (pokemons.Length > 6)
                        {
                            graph.DrawImageUnscaled(Properties.Resources.pmball2b, Properties.Resources.pmball2b.Width * i, 8);
                        }
                        else
                        {
                            graph.DrawImageUnscaled(Properties.Resources.pmballb, Properties.Resources.pmballb.Width * i, 3);
                        }
                    }
                    else
                    {
                        if (pokemons.Length > 6)
                        {
                            graph.DrawImageUnscaled(Properties.Resources.pmball2, Properties.Resources.pmball2.Width * i, 8);
                        }
                        else
                        {
                            graph.DrawImageUnscaled(Properties.Resources.pmball, Properties.Resources.pmball.Width * i, 3);
                        }
                    }
                }

                for (int i = 0; i < pokemons2.Length; i++)
                {
                    if (pokemons2[i].HealthPower == 0)
                    {
                        graph.DrawImageUnscaled(Properties.Resources.pmball2c, 256 - Properties.Resources.pmball2c.Width * (pokemons.Length - i), 3);
                    }
                    else if (pokemons2[i].State != PokemonState.No)
                    {
                        graph.DrawImageUnscaled(Properties.Resources.pmball2b, 256 - Properties.Resources.pmball2c.Width * (pokemons.Length - i), 3);
                    }
                    else
                    {
                        graph.DrawImageUnscaled(Properties.Resources.pmball2, 256 - Properties.Resources.pmball2c.Width * (pokemons.Length - i), 3);
                    }
                }
            }
            return newImg;
        }

        public static Bitmap GetTerrainImage(this BattleTerrain terrain)
        {
            switch (terrain)
            {
                case BattleTerrain.Stadium:
                    return Properties.Resources.建筑;
                case BattleTerrain.Grass:
                    return Properties.Resources.草丛;
                case BattleTerrain.Flat:
                    return Properties.Resources.平地;
                case BattleTerrain.Sand:
                    return Properties.Resources.沙地;
                case BattleTerrain.Mountain:
                    return Properties.Resources.岩山;
                case BattleTerrain.Cave:
                    return Properties.Resources.洞窟;
                case BattleTerrain.Water:
                    return Properties.Resources.水域;
                default:
                    return Properties.Resources.雪原;
            }
        }

        #endregion

        #region 自然恩惠


        //http://wiki.52poke.com/wiki/自然恩惠


        public static byte GetNaturalGiftPower(int berryId)
        {
            if ((1 <= berryId && berryId <= 16) || (36 <= berryId && berryId <= 52))
            {
                return 60;
            }
            else if (17 <= berryId && berryId <= 32)
            {
                return 70;
            }
            return 80;
        }


        public static BattleType GetNaturalGiftType(int BerryIndex)
        {
            int[] types = { 10, 11, 13, 12, 15, 2, 4, 5, 3, 14, 7, 6, 8, 16, 17, 9, 1 };

            if (1 <= BerryIndex && BerryIndex <= 16)
            {
                return (BattleType)types[BerryIndex - 1];
            }
            else if (17 <= BerryIndex && BerryIndex <= 32)
            {
                return (BattleType)types[BerryIndex - 17];
            }
            else if (33 <= BerryIndex && BerryIndex <= 35)
            {
                return (BattleType)types[BerryIndex - 33];
            }
            else if (36 <= BerryIndex && BerryIndex <= 52)
            {
                return (BattleType)types[BerryIndex - 36];
            }
            else if (53 <= BerryIndex && BerryIndex <= 60)
            {
                return (BattleType)types[BerryIndex - 50];
            }
            else
            {
                return BattleType.Normal;
            }
        }

        #endregion

        #region enum helper

        /// <summary>
        /// 技能 保护色
        /// </summary>
        public static BattleType GetPokeType(this BattleTerrain terrain)
        {
            switch (terrain)
            {
                case BattleTerrain.Stadium:
                    return BattleType.Normal;
                case BattleTerrain.Grass:
                    return BattleType.Grass;
                case BattleTerrain.Flat:
                    return BattleType.Normal;
                case BattleTerrain.Sand:
                    return BattleType.Ground;
                case BattleTerrain.Mountain:
                case BattleTerrain.Cave:
                    return BattleType.Rock;
                case BattleTerrain.Water:
                    return BattleType.Water;
                default:
                    return BattleType.Ice;
            }
        }

        public static Image GetTypeImage(int type)
        {
            return PBO.Game.Img.ImageHelper.GetImage((BattleType)type);
        }

        public static Image GetCategoryImage(int category)
        {
            var rm = new ResourceManager(typeof(PBO.Game.Properties.Resources));
            return (Bitmap)rm.GetObject("Move_Category_" + (category + 1));
        }

        public static Image GetImage(this BattleType type)
        {
            var rm = new ResourceManager(typeof(PBO.Game.Properties.Resources));
            return (Bitmap)rm.GetObject("Type_" + ((int)type).ToString("00"));
        }

        private static List<Image> AvatarList;

        public static List<Image> GetAvatarList()
        {
            if (AvatarList == null)
            {
                var rm = new ResourceManager(typeof(PBO.Game.Properties.Resources));
                AvatarList = new List<Image>();
                for (int i = 1; i < 16; i++)
                {
                    var img = (Bitmap)rm.GetObject("Pokemon" + i.ToString("00"));
                    AvatarList.Add(img);
                }
            }
            return AvatarList;
        }

        #endregion

        #region image types

        private static ImageList ImgTypes = null;

        public static ImageList GetTypeList()
        {
            if (ImgTypes == null)
            {
                ImgTypes = new ImageList();
                ImgTypes.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
                ImgTypes.ImageSize = new Size(32, 14);
                ImgTypes.TransparentColor = System.Drawing.Color.Transparent;
                ImgTypes.Images.AddRange(Enum.GetValues(typeof(BattleType)).Cast<BattleType>().Select(t => t.GetImage()).ToArray());
            }
            return ImgTypes;
        }

        #endregion

    }
}
